#!/usr/bin/env node

/**
 * MCP Schema Generator 使用示例
 * 
 * 这个示例展示了如何使用SchemaGenerator类直接生成JSON Schema
 */

import { SchemaGenerator } from '../dist/schema-generator.js'
import path from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

async function example() {
  console.log('🎯 MCP Schema Generator 使用示例\n')

  // 1. 初始化生成器
  const projectPath = path.join(__dirname, '../../') // 指向formily项目根目录
  const generator = new SchemaGenerator(projectPath)

  console.log(`📁 项目路径: ${projectPath}\n`)

  // 2. 扫描所有组件
  console.log('🔍 扫描组件...')
  const components = await generator.scanSchemas()
  console.log(`✅ 找到 ${components.length} 个组件\n`)

  // 3. 显示前几个组件的信息
  console.log('📋 组件列表:')
  components.slice(0, 10).forEach((comp, index) => {
    console.log(`${index + 1}. ${comp.name} (${comp.category || 'common'})`)
    if (comp.description) {
      console.log(`   描述: ${comp.description}`)
    }
  })
  if (components.length > 10) {
    console.log(`   ... 还有 ${components.length - 10} 个组件`)
  }
  console.log()

  // 4. 为Input组件生成JSON Schema
  console.log('🎨 为Input组件生成JSON Schema...')
  const inputComponent = components.find(c => c.name === 'Input')
  if (inputComponent) {
    const jsonSchema = generator.convertToJsonSchema(inputComponent.schema, {
      includeFormilyProps: true,
      includeValidation: false,
      includeReactions: false
    })

    console.log('✅ 生成的JSON Schema:')
    console.log(JSON.stringify(jsonSchema, null, 2))
    console.log()
  }

  // 5. 分析依赖关系
  console.log('🔗 分析组件依赖关系...')
  const dependencies = await generator.analyzeDependencies()
  const componentsWithDeps = Object.entries(dependencies).filter(([_, deps]) => deps.length > 0)

  console.log(`📦 ${componentsWithDeps.length} 个组件有依赖关系:`)
  componentsWithDeps.slice(0, 5).forEach(([component, deps]) => {
    console.log(`   - ${component}: ${deps.join(', ')}`)
  })
  if (componentsWithDeps.length > 5) {
    console.log(`   ... 还有 ${componentsWithDeps.length - 5} 个组件有依赖`)
  }
  console.log()

  // 6. 生成所有组件的Schema并保存
  console.log('💾 生成所有组件的Schema...')
  const allSchemas = await generator.generateAllSchemas({
    includeFormilyProps: false,
    includeValidation: false,
    includeReactions: false
  })

  const outputPath = path.join(__dirname, 'generated-schemas.json')
  await generator.saveSchemas(allSchemas, outputPath)

  console.log(`✅ 成功生成 ${Object.keys(allSchemas).length} 个组件的Schema`)
  console.log(`📄 保存到: ${outputPath}`)
  console.log()

  // 7. 显示统计信息
  console.log('📊 统计信息:')
  console.log(`   总组件数: ${components.length}`)
  console.log(`   有依赖的组件: ${componentsWithDeps.length}`)
  console.log(`   生成的Schema: ${Object.keys(allSchemas).length}`)

  const categories = {}
  components.forEach(comp => {
    const category = comp.category || 'common'
    categories[category] = (categories[category] || 0) + 1
  })

  console.log('   按分类统计:')
  Object.entries(categories).forEach(([category, count]) => {
    console.log(`     ${category}: ${count} 个`)
  })
}

// 运行示例
example().catch(console.error)
